package users

import (
	"../common"
	"../web"
	"encoding/json"
	"strconv"
	"strings"
)

type UserSplitPageObj struct {
	common.BaseHandler
}

type UserDetailsObj struct {
	common.BaseHandler
}

type UserStationObj struct {
	common.BaseHandler
}

func (self *UserSplitPageObj) ParameterCheck() bool {
	s_page := self.Ctx.Params["pageid"]
	pageid, err := strconv.Atoi(s_page)
	if err != nil {
		return false
	}

	idx := 10 * (pageid - 1)
	self.Sqlparas = append(self.Sqlparas, idx)

	return true
}

func (self *UserSplitPageObj) SqlQueryExecute() {
	users := *common.FetchAllRowsPtr(
		`SELECT TOP 10 A.AUTOID, A.USRID, A.NAME, ISNULL(A.DEPARTMENT, ''), A.EXPIRED
		   FROM (
		   			SELECT ROW_NUMBER() OVER(ORDER BY ID) AS AUTOID, ID AS USRID, NAME, DEPARTMENT,
		            	CASE EXPIRED WHEN 0 THEN 'N' ELSE 'Y' END AS EXPIRED
		           FROM SFC_EMPLOYEE(NOLOCK)
		   		) A
		  WHERE A.AUTOID > ?`,
		common.ConnectString, new(common.Employee), self.Sqlparas...)

	dict_js, _ := json.Marshal(map[string]interface{}{"users": users, "lang": self.Lang})
	self.Ctx.ResponseWriter.Write(dict_js)
}

func UserSplitPageHandler(ctx *web.Context) {
	self := new(UserSplitPageObj)
	self.Ctx = ctx
	self.DoJsonQuery(self)
}

func (self *UserDetailsObj) ParameterCheck() bool {
	s_usrd := self.Ctx.Params["userid"]

	self.Sqlparas = append(self.Sqlparas, s_usrd)

	return true
}

func (self *UserDetailsObj) SqlQueryExecute() {
	var usrs [][5]interface{}
	sRtnData := *common.FetchAllRowsPtr(
		`SELECT ROW_NUMBER() OVER(ORDER BY C.RIGHTSID),A.ID,A.NAME,C.RIGHTSURL
           FROM SFC_EMPLOYEE A(NOLOCK),SFC_USERRIGHTS B(NOLOCK),SFC_RIGHTS C(NOLOCK)
          WHERE A.ID=? 
            AND A.ID=B.USERID 
            AND B.RIGHTSID=C.RIGHTSID`,
		common.ConnectString, new(common.EmployeeRights), self.Sqlparas...)
	for _, val := range sRtnData {
		rights := strings.TrimSpace(val.(common.EmployeeRights).Rights)
		if self.Lang == "zh_CN" {
			v, ok := common.Url_Chn[strings.TrimSpace(val.(common.EmployeeRights).Rights)]
			if ok {
				rights = v
			}
		} else {
			v, ok := common.Url_Eng[strings.TrimSpace(val.(common.EmployeeRights).Rights)]
			if ok {
				rights = v
			}
		}
		usrs = append(usrs, [5]interface{}{val.(common.EmployeeRights).Index,
			val.(common.EmployeeRights).Id, val.(common.EmployeeRights).Name, rights,
			strings.TrimSpace(val.(common.EmployeeRights).Rights)})
	}

	self.TmplData["usrs"] = usrs
}

func UsrDetailHandler(ctx *web.Context) {
	self := new(UserDetailsObj)
	self.Ctx = ctx
	self.Html = "usrdetail.html"
	self.Rightlist = []string{"adduser", "modifyuser"}
	self.DoQueryUrl(self)
}

func (self *UserStationObj) ParameterCheck() bool {
	s_usrd := self.Ctx.Params["q"]

	self.Sqlparas = append(self.Sqlparas, s_usrd)

	return true
}

func (self *UserStationObj) SqlQueryExecute() {
	usrs := *common.FetchAllRowsPtr(
		`SELECT ROW_NUMBER() OVER(ORDER BY B.IDX),A.ID,A.NAME,B.STATION
           FROM ShopFloorControl.dbo.SFC_EMPLOYEE A(NOLOCK),SFC_USERSTATION B(NOLOCK)
          WHERE A.ID=? 
            AND A.ID=B.USERID`,
		self.Usr.Conn, new(common.EmployeeRights), self.Sqlparas...)
	self.TmplData["usrs"] = usrs
}

func UsrStationHandler(ctx *web.Context) {
	self := new(UserStationObj)
	self.Ctx = ctx
	self.Html = "usrstation.html"
	self.Rightlist = []string{"adduser", "modifyuser"}
	self.DoQueryUrl(self)
}
